import router, { resetRouter } from './router'
import store from './store'
import storage from 'store'
import NProgress from 'nprogress' // progress bar
import '@/components/NProgress/nprogress.less' // progress bar custom style
import notification from 'ant-design-vue/es/notification'
import { setDocumentTitle, domTitle } from '@/utils/domUtil'
import { ACCESS_TOKEN, CURRENT_PROJECT } from '@/store/mutation-types'
import { i18nRender } from '@/locales'

NProgress.configure({ showSpinner: false }) // NProgress Configuration

const allowList = ['login', 'attestation', 'Person', 'reset', 'jumpMp', 'Clause', 'Privacy'] // 免登录页面地址
const projectList = ['/cost-apply/apply-details', '/overview/detail', '/overview/detail/treasure', '/overview/detail/treasure/payment-detail', '/overview/detail/treasure/finance-detail', '/cost-auditing', '/cost-apply/researcher-list', '/cost-apply/add-researcher', '/cost-apply/edit-researcher', '/cost-apply/subject-list', '/cost-apply/add-subject', '/cost-apply/edit-subject', '/cost-apply/subject-details', '/suditingDetail', '/PayList', '/PayDetail']
const loginRoutePath = '/user/login'
const defaultRoutePath = '/'

router.beforeEach((to, from, next) => {
  NProgress.start() // start progress bar
  to.meta && (typeof to.meta.title !== 'undefined' && setDocumentTitle(`${i18nRender(to.meta.title)} - ${domTitle}`))
  /* has token */
  if (allowList.includes(to.name)) {
    // 在免登录名单，直接进入
    next()
  } else {
    if (storage.get(ACCESS_TOKEN)) {
      if (to.path === loginRoutePath) {
        next({ path: defaultRoutePath })
        NProgress.done()
      } else {
        // 未进入项目 拉取登录权限
        if (to.name === 'All' && from.name !== 'All' && store.getters.project != null) {
          store.dispatch('ClearProject')
        }
        if (!store.getters.pms || store.getters.pms.length === 0) {
          let proId = null
          if (store.getters.project != null) {
            proId = store.getters.project.id
          } else {
            // 修复 在项目列表页面 重新加载页面 bootstrap Initializer 方法 还未执行时 获取不到 proId
            const pro = storage.get(CURRENT_PROJECT)
            if (pro != null && pro.length > 0) {
              const project = JSON.parse(pro)
              proId = project.id
            }
          }
          if (!proId && projectList.includes(to.path)) {
            // 如果没有项目id 且要跳转到项目详情 会报很多参数错误 强制返回项目列表
            next({ path: defaultRoutePath })
            NProgress.done()
          } else {
            if (store.getters.userInfo != null && store.getters.userInfo.isAdmin === 1) {
              // 超级管理员 不传项目id 获取所有权限
              proId = ''
            }
              store
              .dispatch('GetInfo', proId)
                .then((menus, pms) => {
                // generate dynamic router
                store.dispatch('GenerateRoutes', menus).then(() => {
                  // 根据roles权限生成可访问的路由表
                  // 动态添加可访问路由表
                  console.log(menus, 'menus')
                  resetRouter()
                  router.addRoutes(store.getters.addRouters)
                  next(to)
                })
              })
              .catch((err) => {
                console.log('###############', err)
                notification.error({
                  message: '错误',
                  description: '请求用户信息失败，请重试'
                })
                // 失败时，获取用户信息失败时，调用登出，来清空历史保留信息
                store.dispatch('Logout').then(() => {
                  next({ path: loginRoutePath, query: { redirect: to.fullPath } })
                })
              })
          }
        } else {
          next()
        }
      }
    } else {
      next({ path: loginRoutePath, query: { redirect: to.fullPath } })
      NProgress.done() // if current page is login will not trigger afterEach hook, so manually handle it
    }
  }
})

router.afterEach(() => {
  NProgress.done() // finish progress bar
})
